package com.scau.controller;

import com.scau.entity.Menu;
import com.scau.entity.MenuVO;
import com.scau.repository.MenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Value("${server.port}")
    private String port;
    @Value("${web.upload-path}")
    private String fileprefix;
    @Autowired
    private MenuRepository menuRepository;


    @GetMapping("/port")
    public String port(){
        return this.port;
    }


    @GetMapping("/findAll/{index}/{limit}")
    public MenuVO findAll(@PathVariable("index") int index, @PathVariable("limit") int limit){
        System.out.println(index + " " +  limit);
        List<Menu> list = menuRepository.findAll(index, limit);
        return new MenuVO(0,"",menuRepository.count(),list);
    }

    @PostMapping(value = "/save")
    public void save(@RequestBody Menu menu){
        menuRepository.save(menu);
    }

    @GetMapping("/findById/{id}")
    public Menu findById(@PathVariable("id") int id){
        return menuRepository.findById(id);
    }

    @PostMapping(value = "/update")
    public void update(@RequestBody Menu user){
        menuRepository.update(user);
    }

}
